/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import androidx.core.content.ContextCompat;

import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;

public class Selector extends Line {
    private static final int INITIAL_ANIMATION_ALPHA = 0x64;
    private static final int ANIMATION_ALPHA_STEP = 2;

    private int animationAlpha;
    private boolean animateArea;
    private boolean visible;

    public Selector(Context context, float x, float y, float thickness) {
        super(context, x, y, thickness);
        setColor(ContextCompat.getColor(context, R.color.selector));
    }

    /**
     * Starts selection area animation.
     */
    public void startAreaAnimation() {
        animateArea = true;
        animationAlpha = INITIAL_ANIMATION_ALPHA;
    }

    /**
     * Stops selection area animation.
     */
    public void stopAreaAnimation() {
        animateArea = false;
    }

    public boolean isAnimating() {
        return animateArea;
    }

    /**
     *   Hide selectionLine
     */
    public void setVisible(boolean v){
        visible = v;
    }

    public boolean isVisible() {
        return visible;
    }

    @Override
    public void clear() {
        super.clear();

        stopAreaAnimation();
        setVisible(false);
    }

    @Override
    void updateDetectPath() {
        detectPath.set(path);
        detectPath.computeBounds(detectRectF, true);
        detectRectF.roundOut(detectRect);
        detectClipRegion.set(detectRect);
        detectRegion.setPath(detectPath, detectClipRegion);
    }

    @Override
    public void render(Canvas canvas, Paint paint, Context context) {
        if (points.isEmpty()) {
            return;
        }

        if(!visible){
            return;
        }

        if (points.size() == 1) {
            paint.setColor(getColor());
            paint.setStyle(Paint.Style.FILL);

            if (animateArea) {
                updateAnimateAlpha();

                paint.setAlpha(animationAlpha);

                if (animationAlpha == 0) {
                    animateArea = false;
                    visible = false;
                }
            }

            final DiffPoint point = points.get(0);
            canvas.drawCircle(point.x, point.y, thickness / 2, paint);

            return;
        }

        if (needUpdatePath) {
            lastPointsSize = 0;
            path.rewind();
            if(points.size()>0)
                path.moveTo(points.get(0).x, points.get(0).y);
            if(Props.getFlavorChipset().equals(Props.Flavor.Chipset.Mstar))//处理19C白板框选崩掉
                updatePathForSeletor();//为Mstar框选制定加速
            else
                updatePath();//default
        }

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setColor(getColor());

        if (animateArea) {
            updateAnimateAlpha();

            paint.setAlpha(animationAlpha);
            paint.setStyle(Paint.Style.FILL);

            if (animationAlpha == 0) {
                animateArea = false;
                visible = false;
                path.rewind();
            }
        } else {
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(thickness);
        }

        canvas.drawPath(path, paint);
    }

    private void updateAnimateAlpha() {
        animationAlpha -= ANIMATION_ALPHA_STEP;
        if (animationAlpha < 0) {
            animationAlpha = 0;
        }
    }

    public boolean isSingleSelect() {
        DiffPoint pFirst = points.get(0);
        DiffPoint pLast = points.get(points.size()-1);
        return (points.size()<=4) && (DiffPoint.length(pFirst.x-pLast.x, pFirst.y-pLast.y)<=8.0f); // Pen size 3
    }
}
